Skip to content

device: upsert on register to enforce one record per vault+party#5

Merged
johnnyluo merged 3 commits intomainfrom
update-deeplink
Feb 25, 2026
Merged

device: upsert on register to enforce one record per vault+party#5
johnnyluo merged 3 commits intomainfrom
update-deeplink

Conversation

@johnnyluo
Copy link
Contributor

Summary

  • Replace unconditional db.Create() in RegisterDevice with FirstOrInit + Save so re-registering the same (vault_id, party_name) updates the existing row instead of creating a duplicate
  • Add a composite uniqueIndex:idx_vault_party on (vault_id, party_name) in DeviceDBModelAutoMigrate will create the DB-level constraint on next startup
  • Switch UnregisterDevice to Unscoped().Delete() (hard delete) to prevent soft-deleted rows from blocking the unique index when a device re-registers
  • Add isDuplicateKeyError helper (MySQL error 1062) to gracefully handle concurrent registration races

Test plan

  • Register the same vault_id + party_name twice with different tokens — confirm only one row exists and the token is updated
  • Register, unregister, then re-register the same vault_id + party_name — confirm re-registration succeeds with no duplicate key error
  • Verify AutoMigrate creates idx_vault_party unique index on a fresh DB
  • If running against an existing DB with duplicates, deduplicate rows before deploying (keep highest id per group)

🤖 Generated with Claude Code

johnnyluo and others added 3 commits February 26, 2026 07:56
Replace unconditional Create with FirstOrInit+Save so that re-registering
the same (vault_id, party_name) updates the token/device_type instead of
creating a duplicate row. Add a composite unique index on (vault_id,
party_name) enforced at the DB level via AutoMigrate. Hard-delete on
unregister to prevent soft-deleted rows from blocking the unique index on
re-registration. Handle concurrent INSERT races via MySQL error 1062.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Use clause.OnConflict so that registering an existing (vault_id, party_name)
always overwrites the token and device_type in a single atomic statement,
including under concurrent requests. Removes the isDuplicateKeyError helper
which is no longer needed.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@johnnyluo johnnyluo merged commit 0abc563 into main Feb 25, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant